package com.demo.function.spring.feign.fallback;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.fastjson.JSON;
import com.demo.function.spring.feign.MiyaFeignClientV2;
import com.demo.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MiyaFeignClientFallbackFactory implements FallbackFactory<MiyaFeignClientV2> {
    @Override
    public MiyaFeignClientV2 create(Throwable cause) {
        log.error("异常信息", cause);

        // 不是sentinel降级异常，直接抛RuntimeException
        if (!(cause instanceof DegradeException)) {
            throw new RuntimeException(cause);
        }
        return new MiyaFeignClientV2() {
            @Override
            public User modifyUserException(User user) {
                log.info("modifyUserException方法fallbackFactory降级处理，user参数：{}", JSON.toJSONString(user));
                return new User().setUserId(99999L).setUserName("降级处理");
            }

            @Override
            public User processLongTime(long timeout) {
                log.info("processLongTime方法fallbackFactory降级处理，timeout参数：{}", JSON.toJSONString(timeout));
                return new User().setUserId(88888L).setUserName("降级处理");
            }
        };
    }
}
